<?php
namespace Common\Controller;
use Common\Controller\CommonController;
class BraintwisterController extends CommonController
{



    /**
     * 设置所有排行的缓存
     */
    public function setrankingcache($appid,$time){

        $where['appid'] = $appid;
        $user = M('Wxusers')->field('nickName,avatarUrl,checkpoint_final')->where($where)->limit(200)->order('checkpoint_final desc')->select();

        S($appid,$user,$time);//默认存储路径是 ./Application/Home/Runtime/Temp

    }

    /**
     * 获取所有排行的缓存
     * @return mixed
     */
    public function getrankingcache($appid){

        $cache = "";
        $time = 7*24*3600;//缓存一周

        $cache = S($appid);

        if (!$cache||$cache==""){
            $this->setrankingcache($appid,$time);
            $cache = S($appid);
        }

        return $cache;
    }

    /**
     * 缓存本周排行
     */
    public function cacheweek($name,$appid){

        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.challenge_week'=>$week,
            'week.appid'=>$appid,
        );

        $list =$info->table('mj_challenge_week as week')->join('mj_wxusers as uesr on  week.wxusers_id = uesr.wxusers_id')->where($where)->field('week.passnum,uesr.nickName,uesr.avatarUrl')->limit(200)->order('week.passnum desc' )->select();
        foreach ($list as $key=>$value){

            $list[$key]['checkpoint_final'] = $list[$key]['passnum']+1;

        }

        S($name,$list,3600);
    }

    /**
     * 获取一周缓存
     * @param $appid
     * @return mixed|string
     */
    public function getweekcache($appid){

        $cache = "";

        $name = $appid.'week';
        $cache = S($name);

        if (!$cache||$cache==""){
            $this->cacheweek($name,$appid);
            $cache = S($name);
        }

        return $cache;

    }

    /*
     * 定时任务
     */
    public function updatecache(){
        return 1;
        $time = 7*24*3600;//一周
         $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        sleep($time);
         file_get_contents($url);

         return 1;
}



}